Ext.define('Service.impPos.controller.ImpPosController', {
	extend: 'WebOs.ex.DefaultController',
	
	views : [
		'Service.impPos.view.InitContainer',
		'Service.impPos.view.AddPosWindow'
	],
	
	stores : [
			'Service.impPos.store.HtImportPosStore'
			,'Service.impPos.store.HtPosImgStore'
			
	],
	
	models : [
			'Service.impPos.model.HtImportPosModel'
			,'Service.impPos.model.HtPosImgModel'
			
	],
	
	refs : [
		//getter setter
	],
	
	imgs : [],
	
	condition : {},
	
	init: function(h,config) {
		var me=this;
		Ext.apply(me,config);
		this.control({
			'button[action=addHtImportPos]' : {
				click : me.onAddPosBtnClickFire
			},
			'button[id=Service_comUser_view_PosSaveBtn]' : {
				click : me.onPosSaveBtnClickFire
			},
			'button[id=Service_comUser_view_PosCancelBtn]' : {
				click : me.onPosCancelBtnClickFire
			},
			'button[id=Service_imgPos_view_imgUploadBtn]' : {
				click : me.onImgUploadBtnClickFire
			},
			'gridpanel[id=Service_impPos_view_PosGrid]' : {
				afterrender : me.afterPosGridRenderFire,
				itemclick : me.onGridItemClickFire
			},
			'combo[module=impPos]' : {
				select : me.onComComboSelectFire
			},
			'button[action=delHtImportPoss]' : {
				click : me.onDelPosBtnClickFire
			}
		});
	},
	onGridItemClickFire : function(cmp,record){
		var me = this;
		Ext.getCmp('Service_impPos_view_PosInfoForm').loadRecord(record);
		var posId = record.get('id');
		if(posId){
			var store = Ext.getCmp('Service_impPos_view_PosImgView').getStore();
			store.resetRequestParams();
			store.addRequestParams({
				posId : posId
			});
			store.load();
		}
		
	},
	
	onDelPosBtnClickFire : function(btn){
		var me = this;
		var sel = Ext.getCmp('Service_impPos_view_PosGrid').getSelectionModel().getSelection();
		if(sel.length < 1){
			Ext.Msg.alert('提示','请至少选中一个区域');
		}else{
			var ids  = [];
			for(var p in sel){
				ids.push(sel[p].get('id'));
			}
			Ext.MessageBox.confirm('操作确认', '确定要删除这'+ids.length+'项吗?', function(btn) {
				if (btn == 'yes') {
					me.request({
						params : {
							ids : Ext.encode(ids),
							module : 'Service_impPos',
							action : 'delHtImportPoss'
						},
						onSuccess : function(){
							Ext.getCmp('Service_impPos_view_PosGrid').getStore().load();
						}
					});
				}
			}, this);
		}
	},
	
	onComComboSelectFire : function(cmp){
		var comId = cmp.getValue();
		if(comId){
			var store = Ext.getCmp('Service_impPos_view_PosGrid').getStore();
			store.resetRequestParams();
			store.addRequestParams({
				areaId : comId
			});
			store.load();
		}
	},
	
	afterPosGridRenderFire : function(cmp){
		var me = this;
		var store = cmp.getStore();
		console.log(me.condition);
		store.addRequestParams(me.condition);
		if(me.condition.areaId){
			var combo = cmp.up('container').down('combo[module=impPos]');
			combo.select(me.condition.areaId);
		}
		store.load();
		me.condition = {};
	},
	
	onImgUploadBtnClickFire : function(btn){
		var me = this;
		var form = btn.up('form').getForm();
		if(form.isValid()){
			form.submit({
				clientValidation : true,
				url : "imgUpload?type=pos",
				waitMsg : '图片上传中',
				success : function(cm, resp) {
					var result = Ext.decode(resp.response.responseText);
					if (result['success'] == "true") {
						var img = result['result'];
						me.imgs.push(img);
						Ext.getCmp('Service_imgPos_view_imgUploadText').setValue('已经上传' + me.imgs.length + '张图片,可以继续上传');
					} else {
						alert("错误的文件类型,只支持png,jpg,jpeg,gif格式");
					}
				}
			});
		}
	},
	
	onPosSaveBtnClickFire : function(btn){
		var me = this;
		var form = btn.up('window').down('form').getForm();
		if(form.isValid()){
			var values = form.getValues();
			Ext.apply(values,{
				module : 'Service_impPos',
				action : 'addHtImportPos',
				imgs : Ext.encode(me.imgs)
			});
			me.request({
				params : values,
				onSuccess : function(resp){
					Ext.getCmp('Service_impPos_view_PosGrid').getStore().load();
					btn.up('window').destroy();
				}
				
			});
		}
	},
	onAddPosBtnClickFire : function(btn){
		this.imgs = [];
		Ext.create('Service.impPos.view.AddPosWindow').show();
	},
	onPosCancelBtnClickFire : function(btn){
		btn.up('window').destroy();
	}
	
});
